import axios, {
  AxiosRequestHeaders,
  AxiosResponse,
  InternalAxiosRequestConfig,
  AxiosRequestConfig,
} from "axios";

import { useCounterStore } from "../current/index";
import { storeToRefs } from "pinia";
import { ElMessage } from "element-plus";
import router from "../router";
type TAxiosOption = {
  timeout: number;
  baseURL: string; 
};

const config: TAxiosOption = {
  timeout: 20000,
  baseURL: "",
};
class Http {
  service;

  /**
   * Creates an instance of Http.
   * @param {TAxiosOption} config
   * @memberof Http
   */
  constructor(config: TAxiosOption) {
    this.service = axios.create(config);

    /* 请求拦截 */
    this.service.interceptors.request.use(
      (config: InternalAxiosRequestConfig) => {
        const store = useCounterStore();
        const { userInfo } = storeToRefs(store);

        // if (!userInfo.value.username && !userInfo.value.password) {
        //   ElMessage.error("登录失效 请重新登录！！");
        //   router.push("/login");
        // }

        return config;
      },
      (error) => {
        console.log(error);

        return Promise.reject(error);
      }
    );

    /* 响应拦截 */
    this.service.interceptors.response.use(
      (response: AxiosResponse<any>) => {
        console.log(response);
        
        try {
          if (response.data.packageContent.packageStatus === 2) {
                router.push('/login')
          }
          if (response.data.packageContent.packageStatus === 0) {
          } else {
            ElMessage.error(`${response.data.packageContent.packageMsg}`);
          }
        } catch (err) {
          console.log(err);
        }

        console.log(response);
        // console.log(config);

        if (response.config.options.isAllData) {
          return response;
        } else {
          return response.data;
        }
      },
      (error) => {
        console.log(error);
        ElMessage.error(error.message);
        return Promise.reject(error);
      }
    );
  }

  async corsApi(
    url: string,
    method: string, 
    params?: any,
    _object = { isLoading: "1", isAllData: false }
  ) {
    console.log(url, method, params, _object);

    const config: AxiosRequestConfig = {
        url: `http://47.92.249.56:9993/api`,
      // url: `/api`,  
      method,
      headers: {
        "Content-Type": "application/json",
      },
      options: _object,
    };

    if (method === "get" || method === "GET") {
      config.params = params;
    } else {
      config.data = params;
    }

    // return axios(config);
    return this.service(config);
  }

  async  website_corsApi(
    url: string,
    method: string, 
    params?: any,
    _object = { isLoading: "1", isAllData: false }
  ) {
    console.log(url, method, params, _object);

    const config: AxiosRequestConfig = {
        url: `http://47.92.249.56:3006${url}`, 
      // url: `/api`,  
      method,
      headers: {
        "Content-Type": "application/json",
      },
      options: _object,
    };

    if (method === "get" || method === "GET") {
      config.params = params;
    } else {
      config.data = params;
    }

    // return axios(config);
    return this.service(config);
  }


 

}

export default new Http(config);
